package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品信息 spu
     * @param key       查询关键字
     * @param page      当前页
     * @param rows      页大小
     * @param saleable  是否上下架
     * @return          返回分页对象: spuDTO
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){
        PageResult<SpuDTO> result = goodsService.querySpuByPage(key,page,rows,saleable);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存Spu
     * @param spuDTO
     * @return
     */
    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新商品上下架
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable
    ){
        goodsService.updateSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spuId查询spuDetail信息
     * @param spuId     spu的id
     * @return          spuDetaile的DTO
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuId(
            @RequestParam("id") Long spuId
    ){
        SpuDetailDTO spuDetailDTO = goodsService.querySpuDetailBySpuId(spuId);
        return ResponseEntity.ok(spuDetailDTO);
    }

    /**
     * 根据spuId查询sku集合
     * @param spuId     spu的id
     * @return          sku集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuListBySpuId(@RequestParam("id") Long spuId){
        List<SkuDTO> skuDTOList = goodsService.querySkuListBySpuId(spuId);
        return ResponseEntity.ok(skuDTOList);
    }


    /**
     * 更新商品
     * @param spuDTO    前端把所有修改信息封装成spuDTO对象
     * @return  无返回值
     */
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spuid查询spu
     * @param id    spuId
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.querySpuById(id));
    }

    /**
     * 根据skuIds批量查询skuDTO
     * @param ids   skuId的集合
     * @return  sku集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(
            @RequestParam("ids") List<Long> ids
    ){
        return ResponseEntity.ok(goodsService.querySkuByIds(ids));
    }

    /**
     * 减库存
     * @param cartMap   商品id及数量 Map
     * @return
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(
            @RequestBody Map<Long,Integer> cartMap
    ){
        goodsService.minusStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }




}
